'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.
/**
 * @fileoverview 获取攻击方的属性数据
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var soldierAttr = require('./soldierAttr');
var heroAttr = require('./heroAttr');
var PlayerManager = require('../../libs/playerManager');
var unionGroupModel = require('../../models/unionGroupModel');
var equipPropertyService = require('../../services/equipPropertyService');
var techService = require('../../services/techService');
var soldierService = require('../../services/soldierService.js');
var unionTechService = require('../../services/unionTechService');
var soldierConf = require('../../config/soldierConf');
var techType = require('../../consts/techType');

/**
 * 获取玩家数据
 * @param  {[type]} hero_type    [description]
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getData = function (role_id, callback) {
    var player_manager = new PlayerManager(role_id);
    var war_role_info = {};
    var union_model = null;
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'bag', 'soldier', 'skill', 'build', 'tech', 'accrued'], cb);
            },
            function (cb) {
                var union_id = player_manager.role.union_id;
                if (union_id !== 0) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_model = model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;
                var soldier_model = player_manager.soldier;
                var skill_model = player_manager.skill;
                var tech_model = player_manager.tech;
                var build_model = player_manager.build;

                war_role_info.name = role_model.getName();
                war_role_info.lv = role_model.getLv();
                war_role_info.job_type = role_model.getJob();
                war_role_info.hero = _getHeroAttr(role_model, bag_model, skill_model, build_model);
                war_role_info.soldier_list = _getSoldierAttr(soldier_model, bag_model, tech_model);
                war_role_info.union_tech_eff = unionTechService.getUnionTechForSoldierEff(union_model);

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                return callback(err);
            }
            callback(null, war_role_info);
        }
    );
};

/**
 * 获取boss战玩家数据
 * @param  {[type]} hero_type    [description]
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
exports.getDataForBossWar = function (role_id, callback) {
    var player_manager = new PlayerManager(role_id);
    var war_role_info = {};
    var union_model = null;
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'bag', 'soldier', 'skill', 'build', 'tech', 'boss', 'accrued'], cb);
            },
            function (cb) {
                var union_id = player_manager.role.union_id;
                if (union_id !== 0) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_model = model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;
                var soldier_model = player_manager.soldier;
                var skill_model = player_manager.skill;
                var tech_model = player_manager.tech;
                var build_model = player_manager.build;
                var boss_model = player_manager.boss;

                war_role_info.name = role_model.getName();
                war_role_info.lv = role_model.getLv();
                war_role_info.job_type = role_model.getJob();
                war_role_info.hero = _getHeroAttr(role_model, bag_model, skill_model, build_model);
                war_role_info.soldier_list = _getBossWarSoldierAttr(soldier_model, bag_model, tech_model, boss_model);
                war_role_info.union_tech_eff = unionTechService.getUnionTechForSoldierEff(union_model);

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                return callback(err);
            }
            callback(null, war_role_info);
        }
    );
};

/**
 * 取联盟名称
 */
var _getUnionNameInfo = function (union_id, cb) {
    if (!union_id) {
        return cb(null, {
            union_name: '',
            union_flag: 0
        });
    }

    unionGroupModel.getById(union_id, function (err, union_group_model) {
        if (!!err) {
            return cb(err);
        }
        if (!union_group_model) {
            return cb(null, {
                union_name: '',
                union_flag: 0
            });
        }
        cb(null, {
            union_name: union_group_model.name,
            union_flag: union_group_model.union_flag
        });
    });
};

/**
 * 获取竞技场玩家数据
 * @param  {[type]} role_id    [角色ID]
 * @param  {[type]} side       [阵营: 1进攻方/2防守方]
 * @return {[type]}              [description]
 */
exports.getDataForArena = function (role_id, side, callback) {
    var player_manager = new PlayerManager(role_id);
    var war_role_info = {};
    var union_model = null;
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'bag', 'soldier', 'skill', 'build', 'tech', 'arena', 'accrued'], cb);
            },
            function (cb) {
                var union_id = player_manager.role.union_id;
                if (union_id !== 0) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_model = model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;
                var soldier_model = player_manager.soldier;
                var skill_model = player_manager.skill;
                var tech_model = player_manager.tech;
                var build_model = player_manager.build;
                var arena_model = player_manager.arena;

                war_role_info.name = role_model.getName();
                war_role_info.lv = role_model.getLv();
                war_role_info.job_type = role_model.getJob();
                war_role_info.vip = role_model.getVip();
                war_role_info.rank = arena_model.rank;
                war_role_info.soldier_power = (side === 1) ? arena_model.att_power : arena_model.def_power;
                // war_role_info.hero = _getHeroAttr(role_model, bag_model, skill_model, build_model);
                war_role_info.soldier_list = _getArenaSoldierAttr(soldier_model, bag_model, tech_model, arena_model, side);
                if (war_role_info.soldier_power === 0) {
                    war_role_info.soldier_power = soldierService.calcSoldierPower(soldier_model, soldier_model.select_soldier_list);
                }
                war_role_info.union_tech_eff = unionTechService.getUnionTechForSoldierEff(union_model);

                _getUnionNameInfo(role_model.union_id, cb);
            },
            function (union_info, cb) {
                war_role_info.union_name = union_info.union_name;
                war_role_info.union_flag = union_info.union_flag;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                return callback(err);
            }
            callback(null, war_role_info);
        }
    );
};

/**
 * 获取远征玩家数据
 * @param  {[type]} role_id    [角色ID]
 * @return {[type]}              [description]
 */
exports.getDataForExpedition = function (role_id, callback) {
    var player_manager = new PlayerManager(role_id);
    var war_role_info = {};
    var union_model = null;
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'bag', 'soldier', 'skill', 'build', 'tech', 'expedition', 'accrued'], cb);
            },
            function (cb) {
                var union_id = player_manager.role.union_id;
                if (union_id !== 0) {
                    unionGroupModel.getById(union_id, function (err, model) {
                        union_model = model;
                        cb();
                    });
                } else {
                    cb();
                }
            },
            function (cb) {
                var role_model = player_manager.role;
                var bag_model = player_manager.bag;
                var soldier_model = player_manager.soldier;
                var skill_model = player_manager.skill;
                var tech_model = player_manager.tech;
                var build_model = player_manager.build;
                var expedition_model = player_manager.expedition;

                war_role_info.name = role_model.getName();
                war_role_info.lv = role_model.getLv();
                war_role_info.job_type = role_model.getJob();
                war_role_info.vip = role_model.getVip();
                // war_role_info.hero = _getHeroAttr(role_model, bag_model, skill_model, build_model);
                war_role_info.soldier_list = _getExpeditionSoldierAttr(soldier_model, bag_model, tech_model, expedition_model);
                war_role_info.union_tech_eff = unionTechService.getUnionTechForSoldierEff(union_model);

                _getUnionNameInfo(role_model.union_id, cb);
            },
            function (union_info, cb) {
                war_role_info.union_name = union_info.union_name;
                war_role_info.union_flag = union_info.union_flag;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                return callback(err);
            }
            callback(null, war_role_info);
        }
    );
};

/**
 * 没有士兵
 * @param  {[type]}   role_id  [description]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
exports.getDataNoSoldier = function (role_id, callback) {
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'bag', 'skill', 'build', 'accrued'], cb);
            }
        ],
        function (err) {
            if (!!err) {
                return callback(err);
            }
            var role_model = player_manager.role;
            var bag_model = player_manager.bag;
            var skill_model = player_manager.skill;
            var build_model = player_manager.build;
            callback(null, {
                name: role_model.getName(),
                lv: role_model.getLv(),
                hero: _getHeroAttr(role_model, bag_model, skill_model, build_model)
            });
        }
    );
};

/**
 * 获取英雄数据
 * @param  {[type]} hero_type    [description]
 * @param  {[type]} hero_lv      [description]
 * @return {[type]}              [description]
 */
var _getHeroAttr = function (role_model, bag_model, skill_model, build_model) {
    return heroAttr.getAttr(role_model, bag_model, skill_model, build_model);
};

/**
 * 获取兵种数据
 * @return {[type]} [description]
 */
var _getSoldierAttr = function (soldier_model, bag_model, tech_model) {
    var soldier_data = [];
    var select_soldier = soldier_model.getSelectSoldierList();
    _.each(select_soldier, function (soldier_type) {
        var soldier_item = soldier_model.getSoldier(soldier_type);
        if (!!soldier_item) {
            var soldier_attr = soldierAttr.getAttr(
                soldier_type,
                soldier_item.getLv(),
                soldier_item.getStar(),
                soldier_item.talent
            );
            techService.getSoldierAttrWithTechEff(soldier_attr, tech_model);
            // soldier_attr.num = soldier_model.getProductedSoldierNum(soldier_type);
            soldier_attr.num = 0;
            soldier_data.push(soldier_attr);
        }
    });
    return soldier_data;
};

/**
 * 获取BOSS战玩家兵种数据
 * @return {[type]} [description]
 */
var _getBossWarSoldierAttr = function (soldier_model, bag_model, tech_model, boss_model) {
    var soldier_data = [];
    var select_soldier = boss_model.select_soldier_list;
    _.each(select_soldier, function (soldier_type) {
        var soldier_item = soldier_model.getSoldier(soldier_type);
        if (!!soldier_item) {
            var soldier_attr = soldierAttr.getAttr(
                soldier_type,
                soldier_item.getLv(),
                soldier_item.getStar(),
                soldier_item.talent
            );
            // soldier_attr.num = soldier_model.getProductedSoldierNum(soldier_type);
            soldier_data.push(soldier_attr);
        }
    });
    return soldier_data;
};

/**
 * 获取竞技场兵种数据(进攻方/防守方)
 * @return {[type]} [description]
 */
var _getArenaSoldierAttr = function (soldier_model, bag_model, tech_model, arena_model, side) {
    var soldier_data = [];
    var select_soldier = (side === 1) ? arena_model.att_soldier_list : arena_model.def_soldier_list;
    if (select_soldier.length === 0) {
        select_soldier = soldier_model.select_soldier_list;
    }
    _.each(select_soldier, function (soldier_type) {
        var soldier_item = soldier_model.getSoldier(soldier_type);
        if (!!soldier_item) {
            var soldier_attr = soldierAttr.getAttr(
                soldier_type,
                soldier_item.getLv(),
                soldier_item.getStar(),
                soldier_item.talent
            );
            techService.getSoldierAttrWithTechEff(soldier_attr, tech_model);
            // soldier_attr.num = soldier_model.getProductedSoldierNum(soldier_type);
            soldier_attr.num = 0;
            soldier_attr.lv = soldier_item.getLv();
            soldier_attr.attr.hp = Math.floor(soldier_attr.attr.hp);
            soldier_data.push(soldier_attr);
        } else {
            soldier_data.push(null);
        }
    });
    return soldier_data;
};

/**
 * 获取远征士兵数据
 * @return {[type]} [description]
 */
var _getExpeditionSoldierAttr = function (soldier_model, bag_model, tech_model, expedition_model) {
    var soldier_data = [];
    var select_soldier = expedition_model.soldier_list;
    _.each(select_soldier, function (soldier_type) {
        var soldier_item = soldier_model.getSoldier(soldier_type);
        if (!!soldier_item) {
            var soldier_attr = soldierAttr.getAttr(
                soldier_type,
                soldier_item.getLv(),
                soldier_item.getStar(),
                soldier_item.talent
            );
            techService.getSoldierAttrWithTechEff(soldier_attr, tech_model);
            // soldier_attr.num = soldier_model.getProductedSoldierNum(soldier_type);
            soldier_attr.num = 0;

            soldier_attr.attr.hp = Math.floor(soldier_attr.attr.hp);

            // 设置剩余血量
            var new_max_hp = soldier_attr.attr.hp;
            var curr_hp = expedition_model.getSoldierCurrHp(soldier_type, new_max_hp);
            if (curr_hp > 0) {
                soldier_attr.attr.curr_hp = curr_hp;
                soldier_data.push(soldier_attr);
            }
        }
    });
    return soldier_data;
};

/**
 * 取出与士兵类型列表对应的士兵信息
 * @param  {[type]} player_manager    [description]
 * @return {[type]} [description]
 */
exports.getSoldierInfoForType = function (soldier_type_list, player_manager) {
    var soldier_model = player_manager.soldier;
    var bag_model = player_manager.bag;
    var tech_model = player_manager.tech;

    if (!soldier_model) {
        console.error('atkData.getSoldierInfoForType(), need load soldier model');
        return null;
    }
    if (!bag_model) {
        console.error('atkData.getSoldierInfoForType(), need load bag model');
        return null;
    }
    if (!tech_model) {
        console.error('atkData.getSoldierInfoForType(), need load tech model');
        return null;
    }

    var soldier_info_list = [];
    _.each(soldier_type_list, function (soldier_type) {
        var soldier_item = soldier_model.getSoldier(soldier_type);
        if (!!soldier_item) {
            var soldier_attr = soldierAttr.getAttr(
                soldier_type,
                soldier_item.getLv(),
                soldier_item.getStar(),
                soldier_item.talent
            );
            techService.getSoldierAttrWithTechEff(soldier_attr, tech_model);
            // soldier_attr.num = soldier_model.getProductedSoldierNum(soldier_type);
            soldier_attr.num = 0;
            soldier_attr.lv = soldier_item.lv;
            soldier_info_list.push(soldier_attr);
        }
    });
    return soldier_info_list;
};
